package com.ny.dao.impl;

import com.ny.dao.OrderDao;
import com.ny.entity.Order;
import com.ny.until.Sqlconnect;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    Connection conn = Sqlconnect.getCont();

    public OrderDaoImpl() throws SQLException, ClassNotFoundException {
    }

    @Override
    public int addOrder(Order order) throws SQLException {
        String  sql = "insert into orders(student_id,product_id,number,total_price) values(?,?,?,?)";
        PreparedStatement pre = conn.prepareStatement(sql);
        pre.setInt(1,order.getStudent_id());
        pre.setInt(2,order.getProduct_id());
        pre.setInt(3,order.getNumber());
        pre.setDouble(4,order.getTotal_price());
        //添加订单
        int i = pre.executeUpdate();
        if (i > 0){
            System.out.println("添加成功");
            return i;
        }else
            System.out.println("添加失败");
        return 0;

    }
        @Override
        public List<Order> findAllOrder() throws SQLException {
            String sql = "select * from orders";
            PreparedStatement pre = conn.prepareStatement(sql);
            ResultSet rs = pre.executeQuery();
            List<Order> orderList = new ArrayList<>();
            while (rs.next()){
                Order order = new Order();
                order.setOrder_id(rs.getInt("order_id"));
                orderList.add(order);
            }
            return orderList;
        }
    @Override
    public Order deleteOrder(String order_id) throws SQLException {
        String sql = "delete from orders where order_id = ?";
        PreparedStatement pre = conn.prepareStatement(sql);
        pre.setString(1,order_id);
        int i = pre.executeUpdate();
        if (i > 0){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
        return null;
    }
    @Override
    public Order updateOrder(Order order) throws SQLException {
        return null;
    }
}
